// 给你一个 m 行 n 列的矩阵 matrix ，请按照 顺时针螺旋顺序 ，返回矩阵中的所有元素


function spiralOrder(matrix: number[][]): number[] {
    let left = 0
    let top = 0
    let right = matrix[0].length - 1
    let bottom = matrix.length - 1
    let ans:number[] = []
    while(left <= right && top <= bottom){
        for(let i = left;i<=right && top<= bottom;i++){
            console.log('top',top,i)
            ans.push(matrix[top][i])
        }
        top++
        for(let i = top;i<=bottom && left<= right;i++){
            console.log('right',i,right)
            ans.push(matrix[i][right])
        }
        right--
        for(let i = right;i>=left && top<= bottom;i--){
            console.log('bottom',bottom,i)
            ans.push(matrix[bottom][i])
        }
        bottom--
        for(let i = bottom;i>=top  && left<= right;i--){
            console.log('left',i,left)
            ans.push(matrix[i][left])
        }
        left++

    }
    return ans
};
// [1,2,3,6,9,8,7,4,5]
// console.log(spiralOrder([[1,2,3],[4,5,6],[7,8,9]]))


//[1,2,3,4,8,12,11,10,9,5,6,7]
console.log(spiralOrder([[1,2,3,4],[5,6,7,8],[9,10,11,12]]))